import pandas as pd
from sklearn.preprocessing import OneHotEncoder,MinMaxScaler,StandardScaler
import numpy as np

df=pd.read_csv('Telco-Customer-Churn-Data.csv')
print(df.info())
# 转换数据类型
df['TotalCharges']=pd.to_numeric(df['TotalCharges'],errors='coerce')
print(df.describe().T)
# 查看异常值
for col in ['SeniorCitizen','tenure','MonthlyCharges','TotalCharges']:
    quarter_up=df[col].quantile(q=0.75)
    quarter_down=df[col].quantile(q=0.25)
    d=quarter_up-quarter_down
    data_top=quarter_up+1.5*d
    data_bottom=quarter_down-1.5*d
    print(len(df[(df[col]>data_top)|(df[col]<data_bottom)]))
# 去除重复值
print(df['customerID'].duplicated().value_counts())
df.drop_duplicates(subset=['customerID'],inplace=True)
# 去除异常值
df=df[(df['tenure']<100)&(df['tenure']>=0)]
# 独热化处理
onehot_list=['MultipleLines','InternetService','OnlineSecurity',
             'OnlineBackup','DeviceProtection','TechSupport',
             'StreamingTV','StreamingMovies','Contract',
             'PaymentMethod']
onehot=OneHotEncoder()
onehot.fit(np.array(df[col]).reshape(-1,1))
new_cols=onehot.get_feature_names([col]).tolist()
# 插入独热化数据
onehot_value=pd.DataFrame(onehot.transform(np.array(df[col]).reshape(-1,1)).toarray(),columns=new_cols)
df[new_cols]=onehot_value
# 删除独热化前原始数据
df.drop(columns=onehot_list,inplace=True)
# 数值化处理
map_dict={'Male':1,'Female':0,'Yes':1,'No':0}
df.replace(map_dict,inplace=True)
# 查看空值
print(df.isna())
print(df.isna().any(axis=0).value_counts())
print(df.isna().any(axis=1).value_counts())
# 填充空值
df.fillna(0,inplace=True)
# 数据归一化处理
df2=df.copy()
cols2one=['tenure','MonthlyCharges','TotalCharges']
for col in cols2one:
    minmax=MinMaxScaler()
    df[col+'_minmax']=minmax.fit_transform(np.array(df2[col]).reshape(-1, 1))
print(df2.describe().T)
# 数据标准化处理
df3=df.copy()
cols2std=cols2one
for col in cols2std:
    standard=StandardScaler()
    df3[col+'_standard']=standard.fit_transform(np.array(df3[col]).reshape(-1,1))
print(df3.describe().T)
